/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.merashehar.servlet.admin;

import com.merashehar.db.util.ConnectionManager;
import com.merashehar.db.util.DatabaseHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author binkam
 */
public class ItemCRUDServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ConnectionManager connectionManager = new ConnectionManager();
            connectionManager.getConnection().setAutoCommit(false);
            DatabaseHandler databaseHandler = new DatabaseHandler();

            String cityId = request.getParameter("city");
            String categoryId = request.getParameter("category");
            String subCategoryId = request.getParameter("subcategory");
            String itemName = request.getParameter("itemname");
            String itemDesc = request.getParameter("itemdesc");
            String itemImgURL = request.getParameter("itemurl");
            String address1 = request.getParameter("addressline1");
            String address2 = request.getParameter("addressline2");
            String phone1 = request.getParameter("phone1");
            String phone2 = request.getParameter("phone2");
            String landmark1 = request.getParameter("landmark1");
            String landmark2 = request.getParameter("landmark2");
            String mobile = request.getParameter("mobile");
            String zip = request.getParameter("zip");

            /**
             * Fist Add Address.
             */
            try {
                StringBuffer addressSQL = new StringBuffer("insert into address (address_id, address_line1, address_line2, city, phone1, phone2, mobile, zip, landmark1, landmark2) values (");
                addressSQL.append("(select max(address_id)+1 from address),");
                addressSQL.append("'").append(address1).append("',");
                addressSQL.append("'").append(address2).append("',");
                addressSQL.append("'").append(cityId).append("',");
                addressSQL.append("'").append(phone1).append("',");
                addressSQL.append("'").append(phone2).append("',");
                addressSQL.append("'").append(mobile).append("',");
                addressSQL.append("'").append(zip).append("',");
                addressSQL.append("'").append(landmark1).append("',");
                addressSQL.append("'").append(landmark2).append("')");
                out.println(addressSQL.toString());
                Statement itemStatement = connectionManager.getConnection().createStatement();
                itemStatement.execute(addressSQL.toString());
                
                ResultSet rs = databaseHandler.getResultSet("select max(address_id) from address",connectionManager.getConnection());
                String addressId = null;
                if(rs.next()){
                    addressId = rs.getString(1);
                }
                
                out.println("Address Id "+addressId);
                StringBuffer itemSQL = new StringBuffer("insert into infoitem (infoitem_id, itemtype_id, item_name, address_id, item_rank, item_image_url,item_description,city_id,subcategory_id) values (");
                itemSQL.append("(select max(infoitem_id)+1 from infoitem),");
                itemSQL.append("1,");
                itemSQL.append("'").append(itemName).append("',");
                itemSQL.append(addressId).append(",");
                itemSQL.append("0,");
                itemSQL.append("'").append(itemImgURL).append("',");
                itemSQL.append("'").append(itemDesc).append("',");
                itemSQL.append("").append(cityId).append(",");
                itemSQL.append("").append(subCategoryId).append(")");
                out.println(itemSQL.toString());
                itemStatement.execute(itemSQL.toString());
                
                
                
                connectionManager.getConnection().commit();
                
                response.sendRedirect("admin/dbform.jsp");
            } catch (SQLException sqle) {
                sqle.printStackTrace();
                connectionManager.getConnection().rollback();
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
        finally{
            response.sendRedirect("admin/dbform.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
